package com.nebula.service.Impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nebula.common.Const;
import com.nebula.common.ServerResponse;
import com.nebula.mapper.FileAndFolderMapper;
import com.nebula.mapper.FoldersMapper;
import com.nebula.pojo.FileAndFolderList;
import com.nebula.pojo.Folders;
import com.nebula.pojo.User;
import com.nebula.service.IFileAndFolderListService;
import com.nebula.util.FileUtil;
import com.nebula.util.PageUtil;
import com.nebula.vo.FileAndFolderListVo;

@Service("fileAndFolderListService")
public class FileAndFolderListServiceImpl implements IFileAndFolderListService {
	
	@Autowired
	private FileAndFolderMapper fileAndFolderMapper;
	@Autowired
	private FoldersMapper foldersMapper;
	
	public List<FileAndFolderListVo> getFileAndFolderList(int pageNum,int pageSize,String username,int parentId,
			HttpServletRequest request){
		Map<String,Object> map = Maps.newHashMap();
		map.put("username", username);
		map.put("parentId", parentId);
		
		PageHelper.startPage(pageNum, pageSize);
		List<FileAndFolderList> fileAndFolderLists = fileAndFolderMapper.selectFileAndFodlerList(map);
		List<FileAndFolderListVo> fileAndFolderListVos = Lists.newArrayList();
		for(FileAndFolderList fileAndFolderList : fileAndFolderLists){
			FileAndFolderListVo fileAndFolderListVo = assembleFileAndFolderListVo(fileAndFolderList);
			fileAndFolderListVos.add(fileAndFolderListVo);
		}
		PageInfo<FileAndFolderList> pageInfo = new PageInfo<FileAndFolderList>(fileAndFolderLists);
		
		Map<String, Object> info = PageUtil.pageInfo(pageInfo);
		request.setAttribute("pageInfo", info);
		request.setAttribute("parentId",parentId);
		
		return fileAndFolderListVos;
		
	}
	
	public ServerResponse getAllFileAndFolderList(String username,int parentId){
		Map<String,Object> map = Maps.newHashMap();
		map.put("username", username);
		map.put("parentId", parentId);
		List<FileAndFolderList> fileAndFolderLists = fileAndFolderMapper.selectFileAndFodlerList(map);
		List<FileAndFolderListVo> fileAndFolderListVos = Lists.newArrayList();
		for(FileAndFolderList fileAndFolderList : fileAndFolderLists){
			FileAndFolderListVo fileAndFolderListVo = assembleFileAndFolderListVo(fileAndFolderList);
			fileAndFolderListVos.add(fileAndFolderListVo);
		}
		return ServerResponse.createBySuccess(fileAndFolderListVos);
	}
	
	public List<FileAndFolderListVo> searchFileAndFolderList(int pageNum,int pageSize,String username,String queries
			,HttpServletRequest request){
		Map<String, Object> map = Maps.newHashMap();
		map.put("username", username);
		map.put("queries", queries);
		
		PageHelper.startPage(pageNum,pageSize);
		List<FileAndFolderList> fileAndFolderLists = fileAndFolderMapper.selectFileAndFolderListByName(map);
		List<FileAndFolderListVo> fileAndFolderListVos = Lists.newArrayList();
		for(FileAndFolderList fileAndFolderList : fileAndFolderLists){
			FileAndFolderListVo fileAndFolderListVo = assembleFileAndFolderListVo(fileAndFolderList);
			fileAndFolderListVos.add(fileAndFolderListVo);
		}
		PageInfo<FileAndFolderList> pageInfo = new PageInfo<FileAndFolderList>(fileAndFolderLists);
		
		Map<String, Object> info = PageUtil.pageInfo(pageInfo);
		request.setAttribute("pageInfo", info);
		
		return fileAndFolderListVos;
	} 
	
	public ServerResponse searchAllFileAndFolderList(String username,String queries){
		Map<String, Object> map = Maps.newHashMap();
		map.put("username", username);
		map.put("queries", queries);
		
		List<FileAndFolderList> fileAndFolderLists = fileAndFolderMapper.selectFileAndFolderListByName(map);
		List<FileAndFolderListVo> fileAndFolderListVos = Lists.newArrayList();
		for(FileAndFolderList fileAndFolderList : fileAndFolderLists){
			FileAndFolderListVo fileAndFolderListVo = assembleFileAndFolderListVo(fileAndFolderList);
			fileAndFolderListVos.add(fileAndFolderListVo);
		}
		
		return ServerResponse.createBySuccess(fileAndFolderListVos);
	}
	
	public List<FileAndFolderListVo> getUrlShareList(String valArr){
		List<FileAndFolderListVo> fileAndFolderListVos = Lists.newArrayList();
		String []str = valArr.split(",");
		for(int i = 0,len = str.length;i < len;i++){
			FileAndFolderList fileAndFolderList = fileAndFolderMapper.selectUrlShareListById(Long.parseLong(str[i]));
			FileAndFolderListVo fileAndFolderListVo = assembleFileAndFolderListVo(fileAndFolderList);
			fileAndFolderListVos.add(fileAndFolderListVo);
		}
		return fileAndFolderListVos;
	}
	
	public List<FileAndFolderListVo> getUrlShareListByParentId(String parentId,String username){
		Integer pid = Integer.parseInt(parentId);
		
		Map<String, Object> map = Maps.newHashMap();
		map.put("username", username);
		map.put("parentId", pid);
		List<FileAndFolderListVo> fileAndFolderListVos = Lists.newArrayList();
		List<FileAndFolderList> fileAndFolderLists = fileAndFolderMapper.selectFileAndFodlerList(map);
		for(FileAndFolderList item : fileAndFolderLists){
			FileAndFolderListVo fileAndFolderListVo = assembleFileAndFolderListVo(item);
			fileAndFolderListVos.add(fileAndFolderListVo);
		}
		
		return fileAndFolderListVos;
	}
	
	public List<FileAndFolderListVo> getFriendShareList(String valArr){
		List<FileAndFolderListVo> fileAndFolderListVos = Lists.newArrayList();
		String []str = valArr.split(",");
		for(int i = 0,len = str.length;i < len;i++){
			FileAndFolderList fileAndFolderList = fileAndFolderMapper.selectUrlShareListById(Long.parseLong(str[i]));
			FileAndFolderListVo fileAndFolderListVo = assembleFileAndFolderListVo(fileAndFolderList);
			fileAndFolderListVos.add(fileAndFolderListVo);
		}
		return fileAndFolderListVos;
	}
	
	private FileAndFolderListVo assembleFileAndFolderListVo(FileAndFolderList fileAndFolderList){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		String []imgTypes = {"jpg","jpeg","gif","png","bmp","ico"};
		String []musicTypes = {"mp3","wav","wma","flac","ape","aac","ogg"};
		String []zipTypes = {"rar","zip","7z","iso","cab","arj","jar","rar5"};
		String []vedioTypes = {"avi","wmv","mpeg","mp4","mov","mkv","flv","f4v","m4v","rmvb",
				"rm","3gp","dat","ts","mts","vob"};
		String []notRead = {"exe","torrent","apk","flac","ape","vsd"};
		
		FileAndFolderListVo fileAndFolderListVo = new FileAndFolderListVo();
		fileAndFolderListVo.setId(fileAndFolderList.getId());
		
		if(!"folder".equals(fileAndFolderList.getFileExtension())){
			fileAndFolderListVo.setName(fileAndFolderList.getName()+"."+fileAndFolderList.getFileExtension());
		}else {
			fileAndFolderListVo.setName(fileAndFolderList.getName());
		}
		
		if(fileAndFolderList.getDirectory() != null){
			Folders folders = foldersMapper.selectByPrimaryKey(fileAndFolderList.getDirectory());
			if("我的文件".equals(folders.getFolderName())){
				fileAndFolderListVo.setDirectory("全部文件");
			}else{
				fileAndFolderListVo.setDirectory(folders.getFolderName());
			}
			fileAndFolderListVo.setParentId(fileAndFolderList.getDirectory());
		}
		
		fileAndFolderListVo.setUpdatedtime(sdf.format(fileAndFolderList.getUpdatedtime()));
		fileAndFolderListVo.setFileExtension(fileAndFolderList.getFileExtension());
		fileAndFolderListVo.setFileShortUrl(fileAndFolderList.getFileShortUrl());
		
		if("folder".equals(fileAndFolderList.getFileExtension())){
			fileAndFolderListVo.setFileSize("-");
		}else {
			fileAndFolderListVo.setFileSize(FileUtil.byteCountToDisplaySize(fileAndFolderList.getSize()));
		}
		
		String lowercaseSuffix = fileAndFolderList.getFileExtension().toLowerCase().trim();
		
		//图片
		for(String type : imgTypes){
			if(lowercaseSuffix.indexOf(type) > -1){
				fileAndFolderListVo.setIcon(Const.IMG_ICON);
				fileAndFolderListVo.setIsImage(1);
				break;
			}
		}
		
		//音乐
		for(String type : musicTypes){
			if(lowercaseSuffix.indexOf(type) > -1){
				fileAndFolderListVo.setIcon(Const.MUSIC_ICON);
				fileAndFolderListVo.setIsMusic(1);
				break;
			}
		}
		
		//压缩文件
		for(String type : zipTypes){
			if(lowercaseSuffix.indexOf(type) > -1){
				fileAndFolderListVo.setIcon(Const.ZIP_ICON);
				fileAndFolderListVo.setIsDocument(1);
				break;
			}
		}
		
		//视频文件
		for(String type : vedioTypes){
			if(lowercaseSuffix.indexOf(type) > -1){
				fileAndFolderListVo.setIcon(Const.VEDIO_ICON);
				fileAndFolderListVo.setIsVideo(1);
				break;
			}
		}
		
		if(fileAndFolderListVo.getIcon() == null || fileAndFolderListVo.getIcon().equals("")){
			if("apk".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.APK_ICON);
			}
			else if("torrent".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.BT_ICON);
			}
			else if("pdf".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.PDF_ICON);
				fileAndFolderListVo.setIsDocument(1);
			}
			else if("xls".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.XLS_ICON);
				fileAndFolderListVo.setIsDocument(1);
			}
			else if("txt".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.TXT_ICON);
				fileAndFolderListVo.setIsDocument(1);
			}
			else if("exe".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.EXE_ICON);
			}
			else if("folder".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.FOLDER_ICON);
				fileAndFolderListVo.setIsFolder(1);
			}
			else if("doc".equals(lowercaseSuffix) || "docx".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.DOC_ICON);
				fileAndFolderListVo.setIsDocument(1);
			}
			else if("ppt".equals(lowercaseSuffix) || "pptx".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.PPT_ICON);
				fileAndFolderListVo.setIsDocument(1);
			}
			else if("vsd".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.VSD_ICON);
			}else if("html".equals(lowercaseSuffix)){
				fileAndFolderListVo.setIcon(Const.HTML_ICON);
				fileAndFolderListVo.setIsDocument(1);
			}
			else {
				fileAndFolderListVo.setIcon(Const.UNKNOWN_ICON);
			}
		}
		
		//exe,bt,apk,flac,ape,vsd等不支持预览
		for(String type : notRead){
			if(lowercaseSuffix.indexOf(type) > -1){
				fileAndFolderListVo.setIsRead(0);
				break;
			}else if("static/image/filetype/unknown.png".equals(fileAndFolderListVo.getIcon())){
				fileAndFolderListVo.setIsRead(0);
			}else {
				fileAndFolderListVo.setIsRead(1);
			}
		}
		
		return fileAndFolderListVo;
	}
	
}
